package cn.com.cdb.ldms.filter.sensitive;

import jakarta.annotation.PostConstruct;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Data
@Configuration
@ConfigurationProperties(prefix = "logging.sensitive")
@NoArgsConstructor
public class SensitiveDataConfig {
    public static final String SENSITIVE_DATA_KEY = "SensitiveData";
    private Map<String, String> rules;

    @PostConstruct
    public void initRules() {
        if (rules != null) {
            rules.forEach((field, rule) -> {
                if (rule.startsWith("@") &&
                        !SensitiveDataUtils.DEFAULT_RULES.containsKey(rule.substring(1))) {
                    throw new IllegalArgumentException(
                            String.format("字段[%s]配置了无效的脱敏规则别名: %s", field, rule));
                }
            });
        }
        SensitiveDataConverter.rules.putAll(rules);
    }

}